package com.zl.test4android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.NinePatch;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	private SeekBar bar;
	private int number;//分成几段
	private Button btn;
	private EditText editText;
	private int screenWidth;//屏幕
	private Drawable drawable;//seekbar的滑块
	private int drawablewidth;//放大后drawable的宽度

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();

	}

	private void initUI() {
		btn = (Button) findViewById(R.id.btn);
		editText = (EditText) findViewById(R.id.ed);
		bar = (SeekBar) findViewById(R.id.mediacontroller_seekbar);
		// 获取屏幕宽（像素，如：480px）
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
					number = Integer.valueOf(editText.getText().toString().trim());
					if (number == 0)
						Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();
					else
						initData();
				} else {
					Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	private void initData() {
		// 获取seekbar的滑块图片
		drawable = getResources().getDrawable(R.drawable.dianji);
		//放大后drawable的宽度
		drawablewidth = zoomDrawable(drawable,screenWidth/number*2,drawable.getIntrinsicHeight()).getIntrinsicWidth();
		// 设置seekbar滑块放大
//		bar.setThumb(zoomDrawable(drawable, screenWidth / number,
//				drawable.getIntrinsicHeight() * 2));
		bar.setThumb(getThumbDrawable(this,R.drawable.dianji,screenWidth/number));
		// seekbar初始化
		bar.setThumbOffset(0);
		// 监听seekbar滑动情况
		bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				if ((int) ((bar.getProgress() / (100 / number)) + 1) == 1) {// 如果为第一段  直接初始化
					bar.setProgress(0);
					return;
				}
				if ((int) ((bar.getProgress() / (100 / number)) + 1) == number) {// 如果为最后一段 直接设到最大值
					bar.setProgress(100);
					return;
				}
				// 一般情况
				bar.setProgress((int) (bar.getProgress() / (100 / number))
						* (100 / number) + drawablewidth / 2 * 100
						/ screenWidth);
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				// 这里可以写  你滑块停止之后的需要做的方法
			}
		});
	}

	@SuppressWarnings("deprecation")
	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		// drawable转换成bitmap
		Bitmap oldbmp = drawableToBitmap(drawable);
		// 创建操作图片用的Matrix对象
		Matrix matrix = new Matrix();
		// 计算缩放比例
		float sx = ((float) w / width);
		float sy = ((float) h / height);
		// 设置缩放比例
		matrix.postScale(sx, sy);
		// 建立新的bitmap，其内容是对原bitmap的缩放后的图
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
				matrix, true);
		return new BitmapDrawable(newbmp);
	}

	public static Bitmap drawableToBitmap(Drawable drawable) {
		// 取 drawable 的长宽
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		// 取 drawable 的颜色格式
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565;
		// 建立对应 bitmap
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 建立对应 bitmap 的画布
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		// 把 drawable 内容画到画布中
		drawable.draw(canvas);
		return bitmap;
	}
	
	public Drawable getThumbDrawable(Context context, int drawableId, int width) {
		Bitmap bmp_9path = BitmapFactory.decodeResource(getResources(),
				drawableId);
		Bitmap bitmap = Bitmap.createBitmap(width, bmp_9path.getHeight(),
				Bitmap.Config.ARGB_8888);
		NinePatch np = new NinePatch(bmp_9path, bmp_9path.getNinePatchChunk(),
				null);
		RectF rectf_9path = new RectF(0, 0, width, bmp_9path.getHeight());
		Canvas canvas = new Canvas(bitmap);
		np.draw(canvas, rectf_9path);
		return new BitmapDrawable(context.getResources(), bitmap);
	}
	
}
